package com.zyw.module.console.qywx.controller;

import com.zyw.module.console.qywx.api.AccessTokenApi;
import com.zyw.module.console.qywx.api.response.WxUserDeatilResponse;
import com.zyw.module.console.user.po.User;
import com.zyw.module.console.user.service.UserService;
import com.zyw.mvc.servlet.BaseController;
import com.zyw.mvc.support.WrappedResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by qx on 2018/1/8.
 */
@RestController
@RequestMapping("wx")
public class WxController extends BaseController {
    @Autowired
    private UserService userService;
    public static String code;

    public static String accessToken;
/*
    private static Cache<String, String> cache = CacheBuilder.newBuilder()
            .maximumSize(100).expireAfterWrite(24, TimeUnit.HOURS)
            .recordStats()
            .build();

    public static String get(final String key) throws ExecutionException {

        String var = cache.get(key, new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("如果没有值,就执行其他方式去获取值");
                if (key.equals("accessToken")){
                    return AccessTokenApi.getAccessToken();
                }else if (key.equals("userid")){
                    return  AccessTokenApi.getUserInfo(accessToken,code);
                }
               return "";
            }
        });
        return   var;
    }

    public static void put(String key, String value) {
        cache.put(key, value);
    }*/




    @RequestMapping("getUserInfo")
    public WrappedResult getUserInfo(String code) throws Exception {

        WxUserDeatilResponse wxUser = AccessTokenApi.getUserInfoDetail(code);

        Map<String,Object> ret = new HashMap();
        User user = userService.findByWxUserid(wxUser.getUserId());
        User user2 = userService.findByUserName(wxUser.getUserId());
        //在业务系统中没有账号自动创建一个,并让用户完善用户信息
        if (user == null&&user2 ==null) {
            user = new User();
            user.setCreateTime(new Date());
            user.setUpdateTime(user.getCreateTime());
            user.setPhone(wxUser.getMobile());
            user.setEmail(wxUser.getEmail());
            user.setWxUserId(wxUser.getUserId());
            user.setSex(wxUser.getGender());
            user.setPic(wxUser.getAvatar());
            user.setNickname(wxUser.getName());
            user.setStatus(1);
            user.setDelFlag("0");
            userService.insert(user);
            ret.put("user",user);
        }else if (user == null && user2!=null){

            //在业务系统中发现有该微信号之前已经在系统中注册过账号,询问用户是否绑定
            ret.put("user",user2);
            ret.put("wxUserId",wxUser.getUserId());



        }else if (user != null && user2 != null){
            //用户已经在业务系统中注册,并且绑定微信,直接返回用户信息
            ret.put("user",user);
        }
        if (ret.size() == 0){
            ret.put("user",user);
        }
        return wrappedSuccessResult(ret);

    }

    @RequestMapping("consummateInfo")//完善用户信息
    @ResponseBody
    public WrappedResult consummateInfo(String username, String password, Integer id) {
        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setPassword(password);
        userService.update(user);
        return wrappedSuccessResult(null);
    }
}
